package kr.co.tomatosystem.pdetools.shell.internal.osx;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import kr.co.tomatosystem.pdetools.shell.internal.AbstractShowInShellHandler;
import kr.co.tomatosystem.pdetools.shell.internal.util.ProcessKiller;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.commands.IElementUpdater;
import org.eclipse.ui.menus.UIElement;

public class ShowInShellHandlerOSX extends AbstractShowInShellHandler implements IElementUpdater {
    @Override
    protected void doReveal(final File file) {
        String script = String.format("tell app \"Finder\" to activate reveal Posix file \"%s\"", file.getAbsolutePath()); //$NON-NLS-1$

        try {
            ProcessBuilder builder = new ProcessBuilder("osascript", "-e", script); //$NON-NLS-1$ //$NON-NLS-2$
            Process process = builder.start();

            // 무슨 이유던 이 프로세스가 제대로 종료되지 않는 경우 5초뒤 강제 종료.
            new ProcessKiller(process).schedule(5000);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("rawtypes")
    @Override
    public void updateElement(UIElement element, Map parameters) {
        element.setText(Messages.ShowInShellHandlerOSX_SHOW_IN_FINDER);
        element.setIcon(ImageDescriptor.createFromURL(getClass().getResource("finder-icon.png")));
    }
}
